package com.hp.service;

import com.hp.dao.HouseMapper;
import com.hp.dao.RentingMapper;
import com.hp.pojo.House;
import com.hp.pojo.Renting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HouseService {
    @Autowired private HouseMapper houseMapper;

    public Map<String ,Object> getAllHouses(){
        HashMap<String,Object> allHouses = new HashMap<>();
        allHouses.put("allHouses",houseMapper.getAllHouses());
        return allHouses;
    }

    public Map<String ,Object> getHousesByUserId(int userId){

        HashMap<String,Object> allHouses = new HashMap<>();
        allHouses.put("Houses",houseMapper.getHouseByUserId(userId));
        return allHouses;
    }

    public Map<String, String> deleteHouseById(int houseId) {

        HashMap<String, String> res = new HashMap<>();
        try {
            houseMapper.deleteHouseById(houseId);
            res.put("message","修改成功");
            return res;
        }
        catch (Exception e){
            res.put("message","修改失败");
            return res;
        }

    }

    public Map<String, Object> addHouse(House house) {
        HashMap<String, Object> res = new HashMap<>();
        try {
            houseMapper.addHouse(house);
            res.put("message","添加成功");
            res.put("house", house);
            return res;
        }
        catch (Exception e){
            res.put("message","添加失败");
            e.printStackTrace();
            return res;
        }
    }

    public Map<String, Object> getAllHousesByAddress(String addr) {
        /*
        找到所有的信息，逐个查找是否包含
         */
        List<House> allhouses = houseMapper.getAllHouses();
        List<House> res = null;
        for(House house:allhouses){
            if(house.getHouseDetailaddress().indexOf(addr) > 0){
                res.add(house);
            }
        }
        HashMap<String,Object> houses_addr = new HashMap<>();
        houses_addr.put("houses",res);
        return houses_addr;
    }

    public Map<String, Object> getRecommendOnHead() {

        List<House> allhouses = houseMapper.getAllHouses();
//        暂时就是随机返回四个
        List<House> res = null;
        for(int i= 0 ; i < 4;i++){
            res.add(allhouses.get(i));
        }
        HashMap<String,Object> houses_rec_head = new HashMap<>();
        houses_rec_head.put("houses",res);
        return houses_rec_head;
    }

    public Map<String, Object> getHouseById(int houseId) {

        HashMap<String,Object> allhouse = new HashMap<>();
        allhouse.put("houses",houseMapper.getHouseById(houseId));
        return allhouse;
    }

    public Map<String, Object> AddVrLinkToHouse(int houseId, String vrLink) {
        Map<String,Object> map=new HashMap<>();
        House house= houseMapper.getHouseById(houseId);
        house.setHouseVrlink(vrLink);
        if (!house.getHouseVrlink().equals("")){
            house.setHouseVr(1);
        }
        else if (house.getHouseVrlink().equals("")){
            house.setHouseVr(0);
        }
        if (houseMapper.updateHouseById(house)==1){
            map.put("state",1);
            map.put("msg","设置成功！");
        }
        else {
            map.put("state",-1);
            map.put("msg","设置失败！");
        }
        return map;
    }

    public Map<String, Object> getHouseBySearch(String searchValue,Boolean isMap4house) {
        /*
        通过计算所有房子详细地址与搜索值的语义相似度
        对所有房子进行排序
         */
        List<House> allH = (List<House>) getAllHouses().get("allHouses");
        System.out.println(allH.toString());

        if(isMap4house){
            allH.sort((x1,x2) -> getScore2(x1,searchValue)
                    - getScore2(x2,searchValue));
        }
        else{
            allH.sort((x1,x2) -> getScore(x1,searchValue)
                    - getScore(x2,searchValue));
        }

        HashMap<String,Object> res = new HashMap<>();
        res.put("allHouses",allH);
        return res;
    }

    private int getScore(House x1, String searchValue) {
        int s1 = getSimilarity(x1.getHouseDetailaddress(),searchValue);
        int s2 = getSimilarity(x1.getHouseCity(),searchValue);
        int s3 = getSimilarity(x1.getHouseRegion(),searchValue);
//        System.out.println(s1+':'+s2+':'+s3);
        if (s1<s2){
            return s2>s3?s2:s3;
        }
        else{
            return s1;
        }
    }

    private int getScore2(House x1, String searchValue) {
        /*
        通过房子的详细地址来找搜索最相似的房子
         */
        String tarAdd = "";
        if(x1.getHouseProvince().indexOf("重庆") >0){
            tarAdd = x1.getHouseCity()+x1.getHouseRegion();
        }
        else{
            tarAdd = x1.getHouseProvince()+x1.getHouseCity()+x1.getHouseRegion();
        }
//        System.out.println(tarAdd);
        return getSimilarity(tarAdd,searchValue);

    }


    private static int getSimilarity(String doc1, String doc2) {
        if (doc1.equals(doc2)){
            return 1000;
        }

        Map<Character,int[]> algMap=new HashMap<>();
        for (int i = 0; i<doc1.length(); i++) {
            char d1 = doc1.charAt(i);
            int[] fq = algMap.get(d1);
            if (fq != null && fq.length == 2) {
                fq[0]++;
            } else {
                fq = new int[2];
                fq[0] = 1;
                fq[1] = 0;
                algMap.put(d1, fq);
            }
        }
        for (int i = 0; i<doc2.length(); i++) {
            char d2 = doc2.charAt(i);
            int[] fq = algMap.get(d2);
            if (fq != null && fq.length == 2) {
                fq[1]++;
            } else {
                fq = new int[2];
                fq[0] = 0;
                fq[1] = 1;
                algMap.put(d2, fq);
            }
        }
        double sqdoc1 = 0;
        double sqdoc2 = 0;
        double denuminator = 0;
        for (Map.Entry entry : algMap.entrySet()) {
            int[] c = (int[]) entry.getValue();
            denuminator += c[0] * c[1];
            sqdoc1 += c[0] * c[0];
            sqdoc2 += c[1] * c[1];
        }
        return (int) (100*denuminator / Math.sqrt(sqdoc1 * sqdoc2));
    }


    public Map<String, Object> getHousesByMap(String address) {
        /*
        通过详细地址找到附近的房子
         */
        return getHouseBySearch(address,true);
    }

}
